package IC.Types;

import IC.AST.ICClass;

public class ClassType extends Type {

	private String className;
	private ClassType superClass;
	private ICClass ast;

	public ClassType(int id, String name, ClassType superClass, ICClass ast) {
		super(id);
		this.className = name;
		this.superClass = superClass;
		this.ast = ast;
		
	}

	public String getName() {
		return className;
	}
	
	public String getSuperClassName() {
		
		return superClass == null ? null : superClass.getName();
	}
	
	public ClassType getSuperClass() {
		return superClass;
	}

	public ICClass getAst() {
		return ast;
	}
	
	public String getStringForTypeTable(){
		if (superClass == null){
			return id + ": Class: " + getName();
		}
		else{
			return id + ": Class: " + getName() + ", Superclass ID: " + superClass.getId();
		}
	}

	@Override
	public String toString(){
		return getName();
	}
	
	@Override	
	public boolean subTypeOf(Type t){
		
		ClassType parentClass = superClass;
		
		
		if (this == t || t instanceof NullType){
			return true;
		}
		
		if (!(t instanceof ClassType)){
			return false;
		}
		
		while (superClass != null && parentClass != null){
			
			if (((ClassType) t).className.equals(parentClass.className)){
				return true;
			}
			else {
				parentClass = (parentClass.getSuperClass());
			}
		}
		
		return false;
	}
}